const http = require('node:http')

const agent = new http.Agent({ keepAlive: true })

const request = http.request({
  agent: agent,
  hostname: 'localhost',
  port: 3000,
  path: '/create-post',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    name: 'John Doe',
  },
})

request.on('response', (response) => {
  console.log('------ STATUS: ------')
  console.log(response.statusCode)

  console.log('------ HEADERS: ------')
  console.log(response.headers)

  console.log('------ BODY: ------')
  response.on('data', (data) => {
    console.log(JSON.parse(data.toString('utf8')))
  })

  response.on('end', () => {
    console.log('No more data in response.')
  })
})

request.end(
  JSON.stringify({
    title: 'Title of my post',
    body: 'This is some text and more.',
  }),
)
